home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / Development Tools & Languages / DTSCPlusLibrary / Sources / UserTermination.cp < prev    next >
Encoding:
Text File  |  1993-01-14  |  5.0 KB  |  166 lines  |  [TEXT/MPS ]

  1. /* _________________________________________________________________________________________________________ //
  2.   Copyright © 1992 Apple Computer, Inc. All rights reserved.
  3.   Macintosh Developer Technical Support.C++ Macintosh Toolbox Framework.
  4.   Programmer: Kent Sandvik
  5.   Date: 9/26/92
  6.   Revision comments are at the end of this file.
  7.   ---
  8.   TUserTermination is a command dot tracking class, used for finding out if the end user wants to stop
  9.   an action. Most of the code is based on earlier snippet by Dave Radcliffe/DTS.
  10.   TUserTermination.cp contains class body information for the TUserTermination class.
  11.   _________________________________________________________________________________________________________ */
  12.  
  13. #ifndef _USERTERMINATION_
  14. #include "UserTermination.h"
  15. #endif
  16.  
  17.  
  18. // _________________________________________________________________________________________________________ //
  19. // TGDevice class member function implementations
  20.  
  21. //    CONSTRUCTORS & DESTRUCTORS
  22. #pragma segment UserTermination
  23. TUserTermination::TUserTermination(char ASCIIValue)
  24. // The default constructor wants an ASCII char, such as '.' as value (that is default).
  25. // It will call the initialize function that will map the ASCII value to a virtual key code.
  26. {
  27.     long auxVersion = 0;
  28.  
  29.     // Initialize fields to known values
  30.     fChar = ASCIIValue;
  31.     fKeyCode = 0;
  32.  
  33.     // Test if A/UX is present or not, needed later for the event handling code
  34.     OSErr anErr = Gestalt(gestaltAUXVersion, &auxVersion);
  35.     if (anErr == gestaltUnknownErr || auxVersion == 0)
  36.         fAUX = false;
  37.     else
  38.         fAUX = true;
  39.  
  40.     // Do any other dynamic initialization
  41.     this->Initialize();
  42. }
  43.  
  44.  
  45. #pragma segment UserTermination
  46. TUserTermination::~TUserTermination()
  47. // Default constructor, nothing just now.
  48. {
  49. }
  50.  
  51.  
  52. //    INITIALIZATION ROUTINES    
  53. #pragma segment UserTermination
  54. void TUserTermination::Initialize()
  55. // Initialize will fetch the KCHR handle, and convert the ASCII character to
  56. // a real virtual key code.
  57. {
  58.     Handle hKCHR = NULL;
  59.     Ptr pKCHR;
  60.     short KCHRId;
  61.     Boolean notDone;
  62.     long state;
  63.     long ktResult;
  64.  
  65.     pKCHR = (Ptr)GetEnvirons(smKCHRCache);        // try to get the pointer directly
  66.     if (!pKCHR)                                    // maybe pre-System7
  67.     {
  68.         KCHRId = (short)GetScript(short(GetEnvirons(smKeyScript)), smScriptKeys);
  69.         hKCHR = GetResource('KCHR', KCHRId);    // get the KCHR resource
  70.         pKCHR = *hKCHR;                            // get the pointer
  71.     }
  72.     notDone = true;
  73.  
  74.     if (pKCHR)
  75.     {
  76.         state = 0;
  77.  
  78.         while ((fKeyCode <= 0x7F) && notDone)
  79.             // loop through all possible keycodes
  80.             {
  81.                 ktResult = KeyTrans((pKCHR), fKeyCode, &state);// get the ASCII value
  82.                 if (((ktResult & ktAscii1Mask) == fChar) || ((ktResult & ktAscii2Mask) == fChar))// check result for desited real char
  83.                     notDone = false;
  84.                 else
  85.                     fKeyCode++;                    // keep looking
  86.             }
  87.     }
  88.     // Clean up
  89.     if (hKCHR)
  90.         ReleaseResource(hKCHR);
  91.  
  92.     if (notDone)
  93.         fKeyCode = -1;                            // set an insane value
  94.  
  95.     return;                                        // job done    
  96. }
  97.  
  98.  
  99. //    MAIN INTERFACE
  100.  
  101. #pragma segment UserTermination
  102. Boolean TUserTermination::Abort()
  103. //    Abort returns true if the user has pressed Command-Period.
  104. //    It does this by walking the event queue.  Walking the event queue is not
  105. //    recommended, but since we want a Command-Period event to take priority over
  106. //    any other events in the queue, looking ahead in the queue is the only way.
  107. //
  108. //    Note that for A/UX, this technique does not work as A/UX does not support
  109. //    the Macintosh event queue structure.  In that case, we will call CheckAUXEventQueue
  110. //    to find the result.
  111. {
  112.     Boolean foundEvent = false;
  113.     EvQElPtr eventQPtr;
  114.     QHdrPtr eventQHdr;
  115.  
  116.     if (fAUX)                                    // we are running A/UX
  117.         foundEvent = this->CheckAUXEventQueue(fKeyCode, cmdKey);
  118.     else
  119.     {
  120.         eventQHdr = GetEvQHdr();                // get the event queue
  121.         eventQPtr = (EvQElPtr)eventQHdr->qHead;    // get the first element
  122.  
  123.         while (eventQPtr &&!foundEvent)
  124.         {
  125.             foundEvent = (((eventQPtr->evtQMessage & keyCodeMask) >> 8) == fKeyCode && (eventQPtr->evtQModifiers & cmdKey));// the event we want + cmd key?
  126.  
  127.             if (!foundEvent)
  128.                 eventQPtr = (EvQElPtr)eventQPtr->qLink;// get the next element in the queue
  129.         }
  130.     }
  131.     return foundEvent;
  132. }
  133.  
  134.  
  135. //    PRIVATE INTERNAL FUNCTIONS
  136.  
  137. #pragma segment UserTermination
  138. Boolean TUserTermination::CheckAUXEventQueue(char keyCode,
  139.                                              short modifiers)
  140. {
  141.     struct
  142.     {
  143.         EventRecord mask;
  144.         EventRecord value;
  145.     } eventFilter;
  146.  
  147.     eventFilter.mask.what = everyEvent;
  148.     eventFilter.value.what = keyDown;            // look for a keydown event
  149.     eventFilter.mask.message = keyCodeMask;
  150.     eventFilter.value.message = keyCode << 8;    // Set keyCode
  151.     eventFilter.mask.modifiers = modifiers;
  152.     eventFilter.value.modifiers = modifiers;    // Set modifiers
  153.     eventFilter.mask.when = 0;
  154.     eventFilter.mask.where.v = 0;                // Zero out other stuff
  155.     eventFilter.mask.where.h = 0;
  156.     return ((AUXDispatch(kAUXFindEvent, (char*) & eventFilter) > 0) && (eventFilter.value.what != nullEvent));// Have A/UX look for us
  157. }
  158.  
  159.  
  160. // _________________________________________________________________________________________________________ //
  161.  
  162. /*    Change History (most recent last):
  163.   No        Init.    Date        Comment
  164.   1            khs        9/26/92        New file
  165. */
  166.